//
//  ByViewShopsEvaluate.m
//  chequanbao_ios
//
//  Created by pro on 17/5/10.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewShopsEvaluate.h"

@implementation ByViewShopsEvaluateItemStar
{
    UIImageView *_starView;
}
- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        _starView = [[UIImageView alloc] initWithFrame:self.bounds];
        _starView.image = [UIImage imageNamed:@"Shops_star_dark"];
        [self addSubview:_starView];
        
    }
    return self;
}
- (void)setSelected:(BOOL)selected {
    
    _selected = selected;
    
    _starView.image = [UIImage imageNamed:_selected ?
                       @"Shops_star_light" :
                       @"Shops_star_dark"];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    self.selected = true;
    
    
    self.selectedBlock ? self.selectedBlock() : nil;
}

@end

// 评价视图
@implementation ByViewShopsEvaluate
{
    
    NSMutableArray *_stars;
}
- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];


        
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    CGFloat x, y, width, height;
    
    /*
     取消和完成按钮
     */
    x = 15;
    y = 15;
    width = 100;
    height = 20;
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setFrame:CGRectMake(x, y, width, height)];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = FONT(16);
    [cancelBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    
    x = 0;
    y = cancelBtn.top;
    width = cancelBtn.width;
    height = cancelBtn.height;
    UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [okBtn setFrame:CGRectMake(x, y, width, height)];
    okBtn.right = self.width - 15;
    [okBtn setTitle:@"完成" forState:UIControlStateNormal];
    okBtn.titleLabel.font = FONT(16);
    [okBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [okBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [okBtn addTarget:self action:@selector(ok) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:okBtn];
    
    
    /*
     label
     */
    x = 0;
    y = cancelBtn.bottom + 15;
    width = self.width;
    height = 20;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    label.font = FONT(16);
    label.text = @"请对该商家评价";
    label.textColor = [UIColor blackColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    
    
    /*
     评分视图
     */
    _stars = [[NSMutableArray alloc] init];
    CGFloat margin = 20;
    width = 30;
    height = 30;
    CGFloat left = (self.width - margin * 4 - width * 5) / 2;
    for (int i = 0; i < 5; i ++) {
        x = left + (width + margin) * i;
        y = label.bottom + 15;
        ByViewShopsEvaluateItemStar *star = [[ByViewShopsEvaluateItemStar alloc] initWithFrame:CGRectMake(x, y, width, height)];
        star.selected = YES;
        [self addSubview:star];
        star.selectedBlock = ^ {
            [_stars enumerateObjectsUsingBlock:^(ByViewShopsEvaluateItemStar * _Nonnull star, NSUInteger idx, BOOL * _Nonnull stop) {
                
                if (idx <= i) {
                    star.selected = YES;
                } else {
                    star.selected = NO;
                }
            }];
        };
        
        [_stars addObject:star];
    }
    
    
    self.height = self.subviews.lastObject.bottom + 30;
    
    
    // 圆角
    UIBezierPath *corPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *corLayer = [CAShapeLayer layer];
    corLayer.path = corPath.CGPath;
    corLayer.backgroundColor = [UIColor whiteColor].CGColor;
    self.layer.mask = corLayer;
}

#pragma mark - getter
- (NSInteger)level {
    
    __block NSInteger level = 0;
    
    [_stars enumerateObjectsUsingBlock:^(ByViewShopsEvaluateItemStar * _Nonnull star, NSUInteger idx, BOOL * _Nonnull stop) {
        
        level += 1;

        if (star.selected == false) {
            *stop = true;
            level -= 1;
        }
    }];
    return level;
}

#pragma mark - 点击事件
- (void)cancel {
    
    !self.cancelBlock ? : self.cancelBlock();

}
- (void)ok {
    
    !self.okBlock ? : self.okBlock();

}
@end
